/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.persistence.memory;

import apus.entity.*;
import apus.persistence.AccountDAO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Maxim Vasilevsky
 * @author Roman Dyatkovsky
 * @since APUS v0.2
 * @see AbstractMemoryDAO
 * @see AccountDAO
 */
public class MemoryAccountDAO extends AbstractMemoryDAO<Account> implements AccountDAO {
    
    private static Map<Integer, Account> dataSource = new HashMap<>();
    private static Integer counter = 1;
    
    @Override
    protected Map<Integer, Account> getDataSource() {
        return dataSource;
    }
    
    @Override
    protected Integer getAndIncreaseCounter() {
        return counter++;
    }
    
    @Override
    public List<Account> readBySubscriber(Integer id) {
        List<Account> accountList = new ArrayList<>();
        for (Account account : dataSource.values()) {
            if ((account.getSubscriber().getId()==id)) {
                accountList.add(account);
            }
        }
        return accountList;
    }
    
    @Override
    public void calculatePrice(Integer id) {
        Account account = read(id);
        account.calculate();
        update(account);
    }
    
    @Override
    public void calculateAllPrices() {
        for (Account account : getDataSource().values()) {
            calculatePrice(account.getId());
        }        
    }
    
    @Override
    public List<Account> findBySubscriber(String parameter) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public int addMonthlyAccounts() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void payAccount(Integer id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public Account readBySubscriberAndPeriod(Subscriber subscriber, Period period) {
        for (Account account : dataSource.values()) {
            if ((account.getSubscriber().equals(subscriber)) && (account.getPeriod().equals(period))) {
                return account;
            }
        }
        return null;
    }
}
